//
// Created by francklinson on 2021/11/20 AT 10:32.
// May Saint Diana bless you!
//

#include <iostream>
#include <vector>
#include <unordered_map>
#include <unordered_set>
#include <queue>
#include <algorithm>
#include <functional>
#include <climits>
#include <string>

using namespace std;

class Solution {
public:
    bool isHappy(int n) {
        unordered_set<int> seen; // 查重即可
        while (n != 1 && !seen.count(n)) {
            seen.insert(n);
            n = change(n);
        }
        return n == 1;
    }

    int change(int n) {
        int ans = 0;
        while (n) {
            int lastPos = n % 10;
            ans += lastPos * lastPos;
            n /= 10;
        }
        return ans;
    }
};

int main() {
    Solution solution;
    cout << solution.isHappy(19) << endl;
    cout << solution.isHappy(2) << endl;
    return 0;
}

